namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Web.Mvc;
    using System.Web.Routing;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web.Routing;
    using Xunit;

    public sealed class SiteMapXmlControllerFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<SiteMapXmlController>()
                .DerivesFrom<Controller>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .Implements<IRegisterRoutes>()
                .IsDecoratedWith<InternalServerErrorAttribute>()
                .Allow("GET, HEAD, OPTIONS")
                .IsDecoratedWith<CacheControlAttribute>()
                .IsDecoratedWith<ContentLanguageAttribute>()
                .IsDecoratedWith<ContentLocationAttribute>()
                .IsDecoratedWith<ContentMD5Attribute>()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new SiteMapXmlController());
        }

        [Fact]
        public void op_Register_RouteCollection()
        {
            RouteCollection routes = new RouteCollection();

            (new SiteMapXmlController() as IRegisterRoutes).Register(routes);

            Assert.Equal<int>(2, routes.Count);

            Assert.Equal<string>("sitemap.xml", (routes[0] as Route).Url);
            Assert.Equal<string>("sitemap.xml", (routes[1] as Route).Url);
        }

        [Fact]
        public void op_Register_RouteCollectionNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new SiteMapXmlController() as IRegisterRoutes).Register(null as RouteCollection));
        }

        [Fact]
        public void op_XmlRepresentation()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<SiteMapUrlSet>(new
                {
                    urn = "urn://xml/sitemap",
                    xml =
                        "<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>" +
                            "<url>" +
                                "<changefreq>weekly</changefreq>" +
                                "<loc>http://example.com/location</loc>" +
                                "<lastmod>1999-12-31T01:02:03</lastmod>" +
                                "<priority>0.5</priority>" +
                            "</url>" +
                        "</urlset>"
                });

                Assert.IsAssignableFrom<XmlSerializationResult>(new SiteMapXmlController().XmlRepresentation());
            }
            finally
            {
                FakeRepository.Reset();
            }
        }
    }
}